<template>
	<view>
		<view class="goods-list">
			<view v-for="(goods,i) in goodList" :key="i" @click="gotoDetail(goods)">
				<my-goods :goods="goods"></my-goods>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				queryObj:{
					query:'',
					cid:'',
					pagenum:1,
					pagesize:10
				},
				goodList:[],
				total:0,
				isLoading:false
			};
		},
		onLoad(options){
			console.log(options.query)
			this.queryObj.query = options.query || ''
			this.queryObj.cid = options.cid || ''
			
			this.getGoodsList()
		},
		methods:{
			async getGoodsList(cb){
				this.isLoading = true
				const {data:res} = await uni.$http.get('/api/public/v1/goods/search')
				this.isLoading = false
				cb && cb()
				if(res.meta.status !==200) return uni.$showMsg()
				//为数据赋值:通过展开运算符，进行新旧数据的拼接
				this.goodList = [...this.goodList,...res.message.goods]
				this.total = res.message.total
			},
			//触底的事件
			onReachBottom(){
				if(this.queryObj.pagenum * this.queryObj.pagesize >=this.total) return uni.shouMsg('数据加载完毕！')
				if(this.isLoading) return
				this.queryObj.pagenum+=1//让页码自增+1
				this.getGoodsList()//重新获取数据
			},
			onPullDownRefresh(){
				//重置关键数据
				this.queryObj.pagenum = 1
				this.total = 0
				this.isLoading = false
				this.goodList = []
				//重新发起数据请求
				this.getGoodsList(()=>uni.stopPullDownRefresh())
			},
			gotoDetail(goods){
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?goods_id=' + goods.goods_id
				})
			}
		},
	}
</script>

<style lang="scss">
.goods-list{
	
}

</style>
